//
//  LiuUserAccount.swift
//  LiuWeiBo
//
//  Created by 刘晓晨 on 2017/3/29.
//  Copyright © 2017年 刘晓晨. All rights reserved.
//

import UIKit

class LiuUserAccount: NSObject,NSCoding {


//    static let userAccountManager = LiuUserAccount()
    
    //令牌
    var access_token:String?
    
    //生命周期
    var expires_in:Int64 = 0 {
        didSet {
            //通过生命周期计算到期时间
            expireDate = Date().addingTimeInterval(TimeInterval(expires_in))
        }
    
    }
    
    //到期时间
    var expireDate:Date?
    
    
    //用户ID
    var uid:Int64 = 0
    
    //用户头像url
    var avatar_large:String?
    
    //用户名
    var name:String?
    
    
    
    
    
    //KVC构造方法
    init(dict:[String:Any]) {
        super.init()
        setValuesForKeys(dict)
    }
    //  使用yyModel需要时init
    override init() {
        super.init()
    }
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        //空实现
    }
    
    //归档
    func encode(with aCoder: NSCoder) {
        aCoder.encode(access_token, forKey: "access_token")
        aCoder.encode(expireDate, forKey: "expireDate")
        aCoder.encode(uid, forKey: "uid")
        aCoder.encode(avatar_large, forKey: "avatar_large")
        aCoder.encode(name, forKey: "name")
    }
    
    //解档
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObject(forKey: "access_token") as? String
        expireDate = aDecoder.decodeObject(forKey: "expireDate") as? Date
        uid = aDecoder.decodeInt64(forKey: "uid")
        avatar_large = aDecoder.decodeObject(forKey: "avatar_large") as? String
        name = aDecoder.decodeObject(forKey: "name") as? String
    }
    
    //存入沙盒
    func saveUserAccount() {
        
        NSKeyedArchiver.archiveRootObject(self, toFile: path)
    }
    
    //从沙河中取出数据
    static func getUserAccount() -> LiuUserAccount? {
        
        let obj = NSKeyedUnarchiver.unarchiveObject(withFile: path)
        return obj as? LiuUserAccount
    }
    
}



extension LiuUserAccount {
    
    
    
}




